﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class cancel_ticket_ans : System.Web.UI.Page
    {
        float charges, tax, sum, change;
        string seat;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 2)
                    {
                        string ticket_no = Session["seat_no"].ToString();
                        string point = @"select *
                            from Table_Sale
                            inner join Table_SaleDetail
	                            on Table_Sale.Sale_id = Table_SaleDetail.Sale_id
	                        inner join Table_Promotion
								on Table_Sale.Promotion_id = Table_Promotion.Promotion_id
							inner join Table_Passenger
								on Table_Sale.Passenger_id = Table_Passenger.Passenger_id
							inner join Table_Category
								on Table_Passenger.Category_id = Table_Category.Category_id
                            where Table_Sale.Sale_id = '" + ticket_no + "'";
                        SqlCommand sqlcomm = new SqlCommand();
                        string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                        SqlConnection sqlconn = new SqlConnection(Connection);
                        SqlDataAdapter da = new SqlDataAdapter(point, sqlconn);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        try
                        {
                            charges = float.Parse(dt.Rows[0]["Price_ticket"].ToString());
                            tax = float.Parse(dt.Rows[0]["tex"].ToString());
                            seat = dt.Rows[0]["Seat_no"].ToString();
                            float count = float.Parse(Session["count"].ToString());
                            float discount_user = float.Parse(dt.Rows[0]["Category_Discount"].ToString());
                            float promotion = float.Parse(dt.Rows[0]["Promotion_discount"].ToString());
                            //sum = (float)(charges + tax);
                            change = charges - ((charges * (promotion + discount_user)) / 100);
                            float change2 = change + tax;
                            sum = (change + tax) - (((change + tax) * count) / 100);
                            TextBox1.Text = seat.ToString();
                            TextBox2.Text = change2.ToString();
                            TextBox4.Text = sum.ToString();
                            TextBox5.Text = count.ToString();
                            Session["count"] = count;
                        }
                        catch (Exception)
                        {
                            ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถคืนตั๋วได้');", true);
                            //Response.Write(@"<script language='javascript'>alert('ไม่สามารถคืนตั๋วได้');</script>");
                        }
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }                
            }            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            float count = float.Parse(Session["count"].ToString());
            count = 100 - count;
            DataClasses1DataContext db = new DataClasses1DataContext();
            string ticket_no = Session["seat_no"].ToString();
            try
            {
                var q = from p in db.Table_Sales
                        where p.Sale_id == ticket_no
                        select p;
                q.ToList().ForEach(c => c.SSID = 4);
                q.ToList().ForEach(c => c.percentage = count); 
                db.SubmitChanges();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('คืนตั๋วเรียบร้อย');", true);
                //Response.Write(@"<script language='javascript'>alert('คืนตั๋วเรียบร้อย');</script>");
                Response.Redirect("cancel_ticket.aspx");
            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถคืนตั๋วได้');", true);                
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถคืนตั๋วได้');</script>");
                Response.Redirect("cancel_ticket.aspx");
            }           
        }
    }
}